package models.my;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import models.user.UserInfo;

import play.Logger;
import play.db.ebean.Model;
import utils.MengException;
//18
/**
 * 我的消息
 * @author zhangpeng
 *
 */

@Entity
@Table(name="my_message")
public class MyMessage extends Model{

	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String code;
	public String username;
	public String message;    //消息标题
	public String content;    //消息内容
	public long create_at = System.currentTimeMillis();

	public static MyMessage getInstances(){
		return new MyMessage();
	}
	
	public static Model.Finder<Long, MyMessage> find = new Model.Finder<Long, MyMessage>(Long.class, MyMessage.class);
	
	/**
	 * 创建用户消息
	 * 
	 * @param message
	 * @return
	 * @throws MengException
	 */
	public boolean createMessage(MyMessage message) throws MengException {

		if (UserInfo.getInstances().verifyUsername(message.username)) {
			throw new MengException("180001", "用户名不存在");
		}

		if (verifyMyMessage(message.code)) {
			throw new MengException("180002", "用户消息已存在");
		}

		try {
			message.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createMessage Exception," + t.getMessage());
			throw new MengException("180003", "用户消息保存失败");
		}
	}
	
	/**
	 * 用户消息修改
	 * @param message
	 * @return
	 * @throws MengException
	 */
	public boolean modifyMessage(MyMessage message) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(message.username)) {
			throw new MengException("180004", "用户名不存在");
		}

		message.id = getMessage(message.code).id;
		try {
			message.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyMessage Exception," + t.getMessage());
			throw new MengException("180005", "用户消息修改失败");
		}
	}
	
	/**
	 * 用户消息删除
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean destroyMessage(String code) throws MengException {
		
		try {
			getMessage(code).delete();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo destroyMessage Exception," + t.getMessage());
			throw new MengException("180006", "用户消息删除失败");
		}
	}
	
	/**
	 * 获取用户消息列表
	 * @param username
	 * @param page
	 * @param size
	 * @return
	 * @throws MengException
	 */
	public List<MyMessage> getMessagePage(String username, int page, int size) throws MengException {
		
		try {
			return find.where().eq("username", username).findPagingList(size).getPage(page).getList();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getMessagePage Exception," + t.getMessage());
			throw new MengException("180007", "获取用户消息列表失败");
		}
	}
	
	/**
	 * 获取用户消息
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public MyMessage getMessage(String code) throws MengException {
		
		try {
			return find.where().eq("code", code).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getMessage Exception," + t.getMessage());
			throw new MengException("180008", "获取用户消息失败");
		}
	}
	
	public boolean verifyMyMessage(String code) throws MengException {
		MyMessage message = find.where().eq("code", code).findUnique();
		if(message == null) {
			return true;
		}
		return false;
	}
	
}
